package com.server.user;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserDAO {
	
	/**
	 * 根据ID查询用户
	 * @param id
	 * @return
	 */
	@Select("select u.* from user u where u.id=#{id}")
	public Map<String, Object> getUserById(@Param("id")String id);

	/**
	 * 判断手机号是否被注册
	 * @param phone
	 * @return
	 */
	@Select("select * from user where phone=#{phone} limit 1")
	public Map<String, Object> checkPhone(@Param("phone")String phone);
	
	/**
	 * 根据OPENID查询用户
	 * @param id
	 * @return
	 */
	@Select("select u.*,o.openid  from user u left join openid2user o on u.id = o.userid where o.openid=#{openid}")
	public Map<String, Object> getUserByOpenid(@Param("openid")String openid);
	
	/**
	 * 查询openid2user表
	 * @param openid
	 * @return
	 */
	@Select("select * from openid2user where openid=#{openid}")
	public Map<String, Object> getOpenid2user(@Param("openid")String openid);
	
	/**
	 * 清空Openid对应关系
	 * @param openid
	 * @return
	 */
	@Update("delete from openid2user where openid=#{openid}")
	public int delOpenid(@Param("openid")String openid);
	
	/**
	 * 更新openid2user表得对应关系
	 * @param id
	 * @param openid
	 * @return
	 */
	@Update("update openid2user set userid=${id} where openid=#{openid} ")
	public int upOpenid2user(@Param("id")String id,@Param("openid")String openid);

	/**
	 * 插入openid2user表数据
	 * @param id
	 * @param openid
	 * @return
	 */
	@Insert("insert into openid2user (openid,userid) values(#{openid},#{id})")
	public int insOpenid2user(@Param("id")String id,@Param("openid")String openid);
	
	/**
	 * 根据用户ID，查询Openid
	 * @param id
	 * @return
	 */
	@Select("select u.id,o.openid from user u left join openid2user o on u.id= o.userid where u.id= #{id} order by o.ctime desc limit 1")
	public Map<String, Object> getUserOpenid(@Param("id")String id);
	
	/**
	 * 根据条件，查询用户
	 * @param phone
	 * @param pwd
	 * @param tjcode
	 * @return
	 */
	public Map<String, Object> getUser(@Param("phone")String phone,@Param("pwd")String pwd,@Param("tjcode")String tjcode);
	
	/**
	 * 根据手机号和推荐码查询用户
	 * @param phone
	 * @param tjcode
	 * @return
	 */
	@Select("select * from user u where phone=#{phone} and tjcode=#{tjcode}")
	public Map<String, Object> getUserByPAT(@Param("phone")String phone,@Param("tjcode")String tjcode);
	
	/**
	 * 更新用户信息
	 * @param map
	 * @return
	 */
	public int upUser(Map<String, Object> map);

	/**
	 * 新增用户
	 * @param map
	 * @return
	 */
	@Insert("insert into user(id,nickname,realname,head,generation,tjcode,tjtree,phone,pwd,tjrcode,pay_pwd,openid)values(#{id},#{nickname},#{realname},#{head},#{generation},#{tjcode},"
			+ "#{tjtree},#{phone},#{pwd},#{tjrcode},#{pay_pwd},#{openid})")
	public int createUser(@Param("id")String id,@Param("nickname")String nickname,@Param("realname")String realname,@Param("head")String head,@Param("generation")String generation
			,@Param("tjcode")String tjcode,@Param("tjtree")String tjtree,@Param("phone")String phone,@Param("pwd")String pwd,@Param("tjrcode")String tjrcode,@Param("pay_pwd")String pay_pwd,@Param("openid")String openid);
	
	
	/**
	 * 查询sysconfig表配置
	 * @return
	 */
	@Select("select * from sysconf ")
	public List<Map<String, Object>> getSysConfig();
	
	/**
	 * 查询序列
	 * @return
	 */
	@Select("select getSequence() val from dual ")
	public Map<String, Object> getSeq();
	
}
